//
//  MetaHttp.swift
//  MetaTest
//
//  Created by yhb on 2024/10/26.
//

import Foundation
import Alamofire
import SwiftyJSON

class MetaHttp {
    var apiKey: String = "Bearer 8d59c7b4-abe5-4363-a36a-d14ff6e1ad54"
    
    //    struct ResponseData: Decodable {
    //
    //    }
    public static func post(url: String, params: [String: Any]?, callback: @escaping (_ result: Any) -> (), failure: @escaping (_ error: Any) -> ()) {
        MetaHttp().request(url: url, params: params ?? [:], callback: callback, failture: failure)
    }
    
    private func request(url: String, params: [String: Any], callback: @escaping(_ result: Any) -> (), failture: @escaping(_ error: Any) -> ()) {
        //        let httpMethod = HTTPMethod.post
        var httpHeaders = HTTPHeaders()
        httpHeaders.add(.contentType("application/json"))
        httpHeaders.add(name: "Authorization", value: apiKey)
        var request = try! URLRequest(url: URL(string: url)!, method: .post, headers: httpHeaders)
        request.httpBody = try? JSONSerialization.data(withJSONObject: params["bodyList"] ?? [])
        //        AF.request(request).responseDecodable(of: ResponseData.self) { response in
        //            switch response.result {
        //            case .success(let data):
        //                break
        //            case .failure(let error):
        //                break
        //            }
        //        }
        AF.request(request).responseJSON { response in
            switch response.result {
            case .success(let json):
                callback(json)
                print("😄\(json)")
            case .failure(let error):
                print("☹️\(error)")
                failture("请求失败")
            }
        }
    }
    

}
